package com.example.zbh.dsliao.ui.activty;

import android.os.Handler;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.model.entity.ContactPeople;
import com.example.zbh.dsliao.ui.adapter.BlackListAdapter;
import com.example.zbh.dsliao.ui.adapter.ContactAdapter;
import com.example.zbh.dsliao.view.CustomChangeDialog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by zbh on 15/12/2.
 */
public class GroupBlackListActivity extends BaseActivity implements View.OnClickListener, BlackListAdapter.OnBlackClickListener {
    private ImageView backSet;
    private RecyclerView recyclerView;
    private GridLayoutManager manager;
    private String id;
    private BlackListAdapter adapter;
    private Handler handler;
    private List<String> blackMember;
    private CustomChangeDialog dialog;
    private TextView black_textView;
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            adapter.addData(blackMember);
        }
    };


    @Override
    protected int getLayOut() {
        return R.layout.activity_black;
    }

    @Override
    protected void initView() {
        backSet = findeView(R.id.backSet);
        recyclerView = findeView(R.id.blackUserList);
        black_textView = findeView(R.id.black_textView);


    }

    @Override
    protected void initData() {
        id = getIntent().getStringExtra("userId");
        manager = new GridLayoutManager(this, 4);
        backSet.setOnClickListener(this);
        handler = new Handler();
        recyclerView.setLayoutManager(manager);
        adapter = new BlackListAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setOnBlackClickListener(this);
        getBlackList();


    }
    private Runnable runnableB = new Runnable() {
        @Override
        public void run() {
            black_textView.setVisibility(View.GONE);
        }
    };
    private Runnable runnableC = new Runnable() {
        @Override
        public void run() {
      black_textView.setVisibility(View.VISIBLE);
        }
    };

    private void getBlackList() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    blackMember = EMGroupManager.getInstance().getBlockedUsers(id);
                    if (blackMember != null && blackMember.size() > 0){
                        handler.post(runnableB);
                    }else {
                        handler.post(runnableC);
                    }
                    handler.post(runnable);
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }

            }
        }).start();


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.backSet:
                finish();
                break;

        }
    }

    @Override
    public void onBlackClickListener(View v, final String username) {
        dialog = new CustomChangeDialog(this, "确定将" + username + "移出黑名单?" , View.GONE, "", "确定", "取消");
        dialog.show();
        dialog.setCustomChangeDialogListener(new CustomChangeDialog.CustomChangeDialogListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()){
                    case R.id.changeTwoBtn:
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    EMGroupManager.getInstance().unblockUser(id,username);
                                    getBlackList();
                                    dialog.dismiss();
                                } catch (EaseMobException e) {
                                    e.printStackTrace();
                                }
                            }
                        }).start();
                        break;
                    case R.id.cancleBtn:
                        dialog.dismiss();
                        break;
                }
            }
        });

    }
}
